package javabase.exception.Exit;

import java.io.FileOutputStream;
import java.io.IOException;

public class ExitHook {

	public ExitHook() {
		
	}

	public static void main(String[] args) throws IOException {
		final FileOutputStream fos = new FileOutputStream("a.bin");
		System.out.println("程序打开物理资源");
		// 为系统注册关闭钩子
		Runtime.getRuntime().addShutdownHook(new Thread() {
			public void run() {
				if (fos != null) {
					try {
						fos.close();
						System.out.println("程序关闭了物理资源");
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		});
		System.exit(0);
	}
}
